package com.baibei.order.detail;

import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baibei.basic.IPresenter;
import com.baibei.model.OrderInfo;
import com.baibei.model.QuotationInfo;
import com.baibei.model.event.UnsubscribeOrderEvent;
import com.baibei.module.AppRouter;
import com.baibei.module.BasicActivity;
import com.baibei.order.R;
import com.baibei.order.R2;
import com.baibei.ui.AppUI;
import com.rae.widget.dialog.DialogBuilder;
import com.rae.widget.dialog.IAppDialog;
import com.rae.widget.dialog.IAppDialogClickListener;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 订单详情
 * created by shenminjie 2017-8-10
 */
@Route(path = AppRouter.ROUTER_ORDER_DETAIL)
public class OrderDetailActivity extends BasicActivity implements OrderDetailContract.OrderDetailView {

    @BindView(R2.id.layout_btn)
    LinearLayout mLayoutBtn;

    private int mType;
    public static final int TYPE_DEFAULT = 0;
    public static final int TYPE_RECORD = 1;

    /**
     * 订单obj
     */
    private OrderInfo mInfo;
    private OrderDetailContract.Presenter mPresenter;
    private OrderDetailFragment mFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        ButterKnife.bind(this);
        mInfo = getIntent().getParcelableExtra("orderInfo");
        mType = getIntent().getIntExtra("type", TYPE_DEFAULT);
        if (mInfo != null) {
            mPresenter = inject(OrderDetailContract.Presenter.class);
        } else {
            AppUI.failed(this, "订单信息不存在");
            finish();
        }
    }

    @Override
    public IPresenter getBasicPresenter() {
        return mPresenter;
    }

    @Override
    public void refresh(SparseArray<QuotationInfo> quotations) {
        mFragment.refresh(quotations);
    }

    @Override
    public void initView() {
        if (mInfo == null) {
            return;
        }
        //按钮显示与否
        mLayoutBtn.setVisibility(mType == TYPE_DEFAULT ? View.VISIBLE : View.GONE);
        if (mType == TYPE_DEFAULT) {
            mFragment = DefaultFragment.newInstance(mInfo);
        } else {
            mFragment = RecordFragment.newInstance(mInfo);
        }
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, mFragment).commitAllowingStateLoss();
    }

    @Override
    public String getOrderId() {
        return mInfo.getOrderid();
    }

    @Override
    public String getArea() {
        return mInfo.getArea();
    }

    @Override
    public void onLoadFailed(String msg) {
        AppUI.failed(this, msg);
    }

    @Override
    public void unsubscribeSucceess() {
        EventBus.getDefault().post(new UnsubscribeOrderEvent(mInfo.getOrderid()));
        finish();
    }

    @Override
    public void showLoading() {
        AppUI.loading(this);
    }

    @Override
    public void dismissLoading() {
        AppUI.dismiss();
    }

    @Override
    public void onUnsubscribeTimeout() {
        AppUI.dismiss();
        AppUI.failed(this, "退订超时");
    }

    @Override
    public void onUnsubscribeSuccess(String msg) {
        finish();
    }


    @OnClick(R2.id.tv_pick_up)
    public void onPickUp() {
        if (mInfo == null) {
            return;
        }
        AppRouter.routeToSettlement(this, mInfo);
    }

    @OnClick(R2.id.tv_unsubscribe)
    public void onUnsubscribe() {
        if (mInfo == null) {
            return;
        }
        new DialogBuilder(getContext()).setMessage(R.string.dialog_title_unsubscribe).positiveButtonClickListener(new IAppDialogClickListener() {
            @Override
            public void onClick(IAppDialog iAppDialog, int i) {
                iAppDialog.dismiss();
                mPresenter.unsubscribeProduct();
            }
        }).negativeButtonClickListener(new IAppDialogClickListener() {
            @Override
            public void onClick(IAppDialog iAppDialog, int i) {
                iAppDialog.dismiss();
            }
        }).show();

    }


    @Override
    protected void onActivityResultOK(int requestCode, Intent data) {
        super.onActivityResultOK(requestCode, data);
        if (requestCode == AppRouter.REQ_SETTLEMENT) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        AppUI.dismiss();
        super.onDestroy();
    }
}
